#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_

#if _MSC_VER >1000
#pragma once
#endif 

#include "Common.h"

static BOOL IsToolHelpSupport()
{
	BOOL bResult(FALSE);
	HMODULE hModToolhelp32Snapshot = NULL;
	PROC pfnCreateToolhelp32Snapshot;

	hModToolhelp32Snapshot=::LoadLibrary("Kernel32.dll");
	if (NULL!=hModToolhelp32Snapshot)
	{
		pfnCreateToolhelp32Snapshot=::GetProcAddress(hModToolhelp32Snapshot,"CreateToolhelp32Snapshot");
		bResult=(NULL!=pfnCreateToolhelp32Snapshot);

		::FreeLibrary(hModToolhelp32Snapshot);
	}

	return bResult;
}

static BOOL IsPsapiSupport()
{
	BOOL bResult(FALSE);
	HMODULE hModPsapi = NULL;
	
	hModPsapi = ::LoadLibrary( "Psapi.dll" );
	bResult = (NULL != hModPsapi);
	if (NULL != hModPsapi)
		::FreeLibrary(hModPsapi);

	return bResult;
}

static BOOL IsWindow9x()
{
	BOOL bResult=FALSE;
	OSVERSIONINFO vi={sizeof(vi)};

	::GetVersionEx(&vi);
	if(vi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
		bResult=TRUE;
	return bResult;
}

static BOOL IsWindowNT4()
{
	BOOL bResult=FALSE;
	OSVERSIONINFO vi={sizeof (vi)};
	
	if(	(vi.dwPlatformId ==VER_PLATFORM_WIN32_WINDOWS )
		&&(vi.dwMajorVersion == 4))
		bResult=TRUE;

	return bResult;

}

//---------------------------------------------------------------------------
// ModuleFromAddress
//
// Returns the HMODULE that contains the specified memory address
//---------------------------------------------------------------------------
static HMODULE ModuleFromAddress(PVOID pv)
{
	MEMORY_BASIC_INFORMATION mbi;

	return ( ::VirtualQuery(pv,&mbi,sizeof(mbi))!=0
		?(HMODULE)mbi.AllocationBase:NULL);
}

//---------------------------------------------------------------------------
// GetProcessHostFullName
//
// Return the path and the name of the current process
//---------------------------------------------------------------------------
static BOOL GetProcessHostFullName(char * pszFullFileName)
{
	DWORD dwResult = 0;
	
	::ZeroMemory((PBYTE)pszFullFileName,MAX_PATH);
	if (TRUE != IsBadReadPtr((PBYTE)pszFullFileName,MAX_PATH))
		dwResult=::GetModuleFileName(
				NULL,
				pszFullFileName,
				MAX_PATH
				);

	return (dwResult != 0);

}
//---------------------------------------------------------------------------
// GetProcessHostName
//
// Return the name of the current process
//---------------------------------------------------------------------------
static BOOL GetProcessHostName(char * pszFullFileName)
{
	BOOL bResult=FALSE;
	char * pdest;

	int ch='\\';

	bResult=::GetProcessHostFullName(pszFullFileName);

	if(bResult){
		pdest=::strrchr(pszFullFileName,ch);
		if(NULL != pdest)
			strcpy(pszFullFileName, &pdest[1]);
	}
	return bResult;
}

//---------------------------------------------------------------------------
// ReplaceFileName
//
//---------------------------------------------------------------------------
static BOOL ReplaceFileName
(
	char * pszOldFileName,
	char * pszNewFileName,
	char * pszBaseNewFileName
	)
{
	BOOL bResult=FALSE;
	char * pdest;

	int ch='\\';

	if (
		(TRUE != ::IsBadReadPtr((PBYTE)pszOldFileName,strlen(pszOldFileName)+1))&&
		(TRUE != ::IsBadReadPtr(pszBaseNewFileName,strlen(pszBaseNewFileName)+1))
		)
	{
		if (TRUE != ::IsBadReadPtr(pszNewFileName,MAX_PATH))
		{
			::ZeroMemory(pszNewFileName,MAX_PATH);
			strcpy(pszNewFileName,pszOldFileName);
			
			pdest = ::strrchr(pszNewFileName,ch);
			if(NULL != pdest)
				strcpy(&pdest[1],pszBaseNewFileName);
			else
				strcpy(pszNewFileName,pszBaseNewFileName);
		}
	}
	return bResult;
}

//---------------------------------------------------------------------------
// ReplaceFileName
//
//---------------------------------------------------------------------------
static BOOL ReplaceFileName
(
	HINSTANCE hOldFile,
	char * pszNewFileName,
	char * pszBaseNewFileName
	)
{
	BOOL bResult=FALSE;
	char szFileName[MAX_PATH];

	if(TRUE != ::IsBadReadPtr(pszBaseNewFileName,sizeof(pszBaseNewFileName)+1))
		if(TRUE != ::IsBadReadPtr(pszNewFileName,MAX_PATH))
		{
			::ZeroMemory(pszNewFileName,MAX_PATH);
			if(NULL != hOldFile)
			{
				if(0 != ::GetModuleFileName(hOldFile,szFileName,MAX_PATH))
				{
					bResult=::ReplaceFileName(szFileName,pszNewFileName,pszBaseNewFileName);
				}
			}
		}
		return bResult;
}
//
// Converts a string to a boolean value
//
static BOOL StrToBool(char * pszValue)
{
	return ( (0 == stricmp("YES", pszValue)) ||
		     (0 == stricmp("Y", pszValue)) ||
		     (0 == stricmp("TRUE", pszValue)) );
}

//
// Converts a boolean value to a string
//
static void BoolToStr(BOOL bValue,char * pszResult)
{
	bValue ? strcpy(pszResult,"Yes"):
			 strcpy(pszResult,"No");
}

//
// Trims leading spaces and control characters from a string
//
static void TrimLeft(
	const char*  pszParam,
	char*        pszResult
	)
{
	char szBuffer[MAX_PATH];
	strcpy(szBuffer, "\0");
	if ( (TRUE != ::IsBadStringPtr(pszParam, MAX_PATH)) &&
		 (strlen(pszParam) > 0) )
	{
		DWORD dwIndex = 0;
		while ( (dwIndex < strlen(pszParam)) && (pszParam[dwIndex] == ' ') )
			dwIndex++;
		if (dwIndex < strlen(pszParam))
			strcpy(szBuffer, &pszParam[dwIndex]);
	} // if
	strcpy(pszResult, szBuffer);
}
	
//
// Trims trailing spaces and control characters from a string
//
static void TrimRight(
	const char*  pszParam,
	char*        pszResult
	)
{
	char szBuffer[MAX_PATH];
	strcpy(szBuffer, "\0");
	if ( (TRUE != ::IsBadStringPtr(pszParam, MAX_PATH)) &&
		 (strlen(pszParam) > 0) )
	{
		int nIndex = strlen(pszParam) - 1;
		while ( (nIndex >= 0) && (pszParam[nIndex] == ' ') )
			nIndex--;
		if (nIndex >= 0)
		{
			memcpy(
				(PBYTE)szBuffer, 
				(PBYTE)pszParam, 
				(nIndex + 1)
				); 
			szBuffer[nIndex+1] = 0;
		} // if
	} // if
	strcpy(pszResult, szBuffer);
}

//
// Trims leading and trailing spaces and control characters from a string
//
static void Trim(
	const char*  pszParam,
	char*        pszResult
	)
{
	TrimLeft(pszParam, pszResult);
	TrimRight(pszParam, pszResult);
}



//
// Return next entry of an comma separated string
//
static BOOL GetNextCommaSeparatedString(
	const char*  pszParam,
	char*        pszResult,
	DWORD        dwLength,
	long*        pnCommaPos
	)
{
	*pnCommaPos = -1;
	BOOL   bResult = FALSE;
	const char*  pdest;
	strcpy(pszResult, "\0");

	if (strlen(pszParam) > 0)
	{
		::ZeroMemory((PBYTE)pszResult, dwLength);

		pdest = strstr(pszParam, ",");
		if (pdest)
			*pnCommaPos = pdest - pszParam - 1;
		else
			*pnCommaPos = strlen(pszParam);
		memcpy(
			(PBYTE)pszResult, 
			(PBYTE)pszParam, 
			((*pnCommaPos) + 1)
			); 
		(*pnCommaPos)++;

		Trim(pszResult, pszResult);

		bResult = TRUE;
	} // if

	return bResult;
}


//---------------------------------------------------------------------------
// UnicodeToAnsi
// 
// Tranlsates Unicode to Ansi strings
//---------------------------------------------------------------------------
static BOOL UnicodeToAnsi(
	LPWSTR pszwUniString, 
	LPSTR  pszAnsiBuff,
	DWORD  dwAnsiBuffSize
	)
{
	int  iRet ;
    iRet = ::WideCharToMultiByte(
		CP_ACP,
		0,
		pszwUniString,
		-1,
		pszAnsiBuff,
		dwAnsiBuffSize,
		NULL,
		NULL
		);
	return (0 != iRet);
}


#endif 